/**
 *
 */
package com.gamex.findit.internal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.gamex.findit.Constants;
import com.gamex.findit.model.Member;

public class LoginCheckInterceptor extends HandlerInterceptorAdapter implements InitializingBean {
	String exclude = "/admin/login";
	String exclude2 = "/app/member";
	String exclude3 = "/resources/";

	@Override
	public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session ;
		Member member;
		 
	try{
		String path = request.getRequestURI();
		
		
		if(!path.contains(exclude) && !path.contains(exclude2) && !path.contains(exclude3)){
				session = request.getSession();
				member = (Member)session.getAttribute(Constants.SESSION_INFO);
				if(member != null){
					if(!member.getUserRole().equals("ADMIN") && path.contains("/admin/")){
						response.sendRedirect(request.getContextPath() +"/index.jsp");
						return false;
					}else{
						return true;
					}
					
				}else{
					response.sendRedirect(request.getContextPath() +"/index.jsp");
					return false;
				}
			}else{
				return true;
			}
	
		}finally{}

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
